package com.tsyun.view_project;

import android.Manifest;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsReaderView;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.io.File;

public class TencentWebViewActivity extends AppCompatActivity {
    private WebView webView;
    //存放TbsReaderView的容器
    private FrameLayout flPreview;
    //TbsReaderView 用来解析本地Office文件
    //支持如下格式文件的预览 doc、docx、ppt、pptx、xls、xlsx、pdf、txt、epub
    private TbsReaderView tbsReaderView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tencent_webview);
        webView = findViewById(R.id.webview);
        flPreview = findViewById(R.id.fl_preview);
//        if (WebApplication.supportX5WebView) {
        tbsReaderView = new TbsReaderView(this, callback);
//        } else {
//            Toast.makeText(this, "您的系统暂不支持文件预览", Toast.LENGTH_SHORT).show();
//        }
        //动态添加tbsReaderView
        flPreview.addView(tbsReaderView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//        webView.setWebViewClient(new WebViewClient());
//        String filePath = Environment.getExternalStorageDirectory()+"/DingTalk/2021年4月班会PPT_18.pptx";
//        String filePath = Environment.getExternalStorageDirectory()+"/DingTalk/米大app下载链接_1.xlsx";
//        String filePath = Environment.getExternalStorageDirectory()+"/ldy/assoNo.pdf";
        //此处一般需要调用手机的文件管理器 去选择文件 此处为了方便 直接写死文件的路径
        String filePath = Environment.getExternalStorageDirectory() + "/Download/人才简历表.docx";
        //判断读取权限存不存在
        PermissionsUtils.getInstance().checkPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionsUtils.IPermissionsResult() {
            @Override
            public void passPermissons() {
//                QbSdk.openFileReader(TencentWebViewActivity.this, filePath, null, new ValueCallback<String>() {
//                    @Override
//                    public void onReceiveValue(String s) {
//                    }
//                });

                File file = new File(filePath);
                if (!file.exists()) {
                    Toast.makeText(TencentWebViewActivity.this, "文件不存在", Toast.LENGTH_LONG).show();
                    return;
                }
                //如果需要预览本地文件 需要传送bundle到TbsReaderView中
                Bundle bundle = new Bundle();
                bundle.putString("filePath", filePath);
                bundle.putString("tempPath", Environment.getExternalStorageDirectory().getPath());
                //此处判断是否支持s参数类型的文件类型，如果支持，则result返true，如果不支持，则返回FALSE，并提示用户，该文件不可预览
                boolean result = tbsReaderView.preOpen("docx", false);
                if (result) {
                    tbsReaderView.openFile(bundle);
                }

            }

            @Override
            public void forbitPermissons() {

            }
        });

    }

    //读取后的回调方法 空实现
    TbsReaderView.ReaderCallback callback = (integer, o, o1) -> {

    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在页面销毁的时候，自动关闭文件预览，此处若不关闭，会消耗系统资源
        if (tbsReaderView != null) {
            tbsReaderView.onStop();
        }
    }

    //请求权限的回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }
}
